package org.sosie2.service.test.mule;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Vector;

import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;

import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.message.MessageElement;
import org.apache.axis.message.PrefixedQName;
import org.apache.axis.message.SOAPEnvelope;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.opensaml.SAMLAttribute;
import org.opensaml.SAMLException;
import org.sosie2.service.framework.AbstractService;
import org.sosie2.service.framework.MuleAbstractService;
import org.sosie2.service.framework.SAMLAnnotation;
import org.sosie2.service.framework.SosieException;
import org.sosie2.service.framework.Utils;
import org.w3c.dom.Document;

/**
 * The service adds a new SOAPBodyElement, generates an annotation and appends it to the modified message.
 * 
 * @author gabriela
 * @author oppy
 */
public class MuleAnnotationService extends MuleAbstractService {
	static Logger logger = Logger.getLogger(MuleAnnotationService.class); 
	
	private String textToAdd = this.getClass().getSimpleName();
	
	public void init() {
		Utils instanceUtils = Utils.get();
		URL url = instanceUtils.getFile("configuration//" + Utils.LOGGER_FILE);
		if (url == null)
			logger.info("URL is NULL" );
		else {	
			PropertyConfigurator.configure(url);
		}		
	}
	
		
	/**
	 *  A simple constructor
	 */
	public MuleAnnotationService() {
		init();
	}
	
	
	public Message invoke(Message req, Message resp) throws Exception {    					
		// Copy Header
		SOAPHeader header = req.getSOAPHeader();
		copyHeader(header, resp.getSOAPHeader());
		
		// Copy Body
		copyBody(req.getSOAPBody(), resp.getSOAPBody());
		
		// Add a tag to the body				
		SOAPBody respBody = resp.getSOAPBody();
		SOAPBodyElement e = respBody.addBodyElement(new PrefixedQName("http://unibw.de/sosie", textToAdd, "sosie"));				
		e.addTextNode("Timestamp: " + (new Date()));
    		
		// Create dummy attributeNames - attributeValues array		
		String[][] att = new String[1][2];
		att[0][0] = "DummyAttribute";
		att[0][1] = "DummyValue";
		
		//attach the hashmap + rest of parameters to the annotation
		SAMLAnnotation sa = new SAMLAnnotation(att, "http://unibw.de/sosie/test/AnnotationServiceTest", "http://unibw.de/sosie/next");
		Document doc = resp.getSOAPEnvelope().getAsDocument();
		doc = addAnnotation(doc, sa, "http://unibw.de/sosie/dummyAnnotationService");
		SOAPMessage soapMsg = Utils.toMessage(doc);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		soapMsg.writeTo(out);
				
		//set the response message 
		Message msg = new Message(new ByteArrayInputStream(out.toByteArray()), false);

		logger.info("finished");
		
		return msg;
    }
	
}
